package normal.链表;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 测试链表
 *
 * @author ZhuYi
 * @create 2024/12/10 16:37
 */
public class main {


    public static void main(String[] args) {
//        //链表初始化
        ListNode l0 = new ListNode(10);
        ListNode l1 = new ListNode(5);
        ListNode l2 = new ListNode(7);
        ListNode l3 = new ListNode(30);
        ListNode l4 = new ListNode(26);
        ListNode l5 = new ListNode(1);
        l0.next = l1;
        l1.next = l2;
        l2.next = l3;
        l3.next = l4;
        l4.next = l5;

//        ListNode access = access(l0, 4);
//        System.out.println(access.val);

//        insert(l0,new ListNode(20));
//        System.out.println(l0.next.val);

//        delete(l0);
//        System.out.println(l0.next.val);

        int i = searchTarget(l0, 26);
        System.out.println();
    }

    //在某节点lx后插入节点p
    public static void insert(ListNode lx, ListNode p) {
        lx.next = p;
        p.next = lx.next.next;
    }

    //删除某节点后的首个节点
    public static void delete(ListNode lx) {
        lx.next = lx.next.next;
    }

    //查找索引为i的节点
    public static ListNode access(ListNode head, int index) {
        for (int i = 0; i < index; i++) {
            if (head == null) return null;
            head = head.next;
        }
        return head;
    }

    //查找值为target的节点并输出它的索引
    public static int searchTarget(ListNode head, int target) {
        int index = 0;
        while (head != null) {
            if (head.val == target) {
                return index;
            }
            head = head.next;
            index++;
        }
        return -1;
    }
}
